package com.example.timedtask;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class Task {

	@Scheduled(cron = "0 0 13 ? * MON-FRI")
	public void taskTime() {
		httpURLConectionGET("http://api.yoogor.com/release/releasePO.aspx");
		httpURLConectionGET("http://api.zhzysj.com/release/releasePO.aspx");
		httpURLConectionGET("http://api.zjahwork.com/release/releasePO.aspx");
		httpURLConectionGET("http://api.yoogor.com:88/release/releasePO.aspx");
	}

	/**
	 * 接口调用 GET
	 */
	public static void httpURLConectionGET(String u) {
		BufferedReader br = null;
		StringBuilder sb = new StringBuilder();
		HttpURLConnection connection = null;
		try {
			URL url = new URL(u); // 把字符串转换为URL请求地址
			connection = (HttpURLConnection) url.openConnection();// 打开连接
			connection.setRequestMethod("GET");
			// 连接会话
			connection.connect();
			// 获取输入流
			br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String line;
			// 循环读取流
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
			System.out.println(sb.toString());
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("失败!");
		} finally {
			try {
				// 关闭输入流
				if (br != null) {
					br.close();
				}
				// 断开连接
				if (connection != null) {
					connection.disconnect();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}
}
